#include <sstream>
#include "../utility/assert_helper.h"
#include "../win_manager/windows_manager.h"
#include "spyware_scanner.h"
#include "main_form.h"

const char	*SPEC_FILE_PATH = "/usr/share/sapper/.temp.dat";

Main_Form::Main_Form() :
context_id_(0), sw_scanner_(NULL)
{
	I_Windows_Manager * win_manager = ctreate_windows_manager();
	if (NULL == win_manager) exit(-1);
	if (0 == win_manager->get_windows_total()) {
		Gtk::MessageDialog no_system(*this, "No MS-Windows found:", false,
		Gtk::MESSAGE_ERROR);
		no_system.set_secondary_text(
          "找不到Windows操作系统\n您是否还没安装Windows系统\n或者尚未挂载全部分区？");
		no_system.run();
		exit(-1);
	}
	set_default_size(600, 400);
	set_title("Spyware Killer");
	sw_scanner_ = new Spyware_Scanner(*(win_manager->get_windows_information(0)), SPEC_FILE_PATH);
	context_id_ = status_bar_.get_context_id("swkiller status bar");
	Gtk::VBox *vbox0 = Gtk::manage(new Gtk::VBox);
	vbox0->set_spacing(5);
	if (NULL != vbox0) {
		add(*vbox0);
		vbox0->pack_start(*Gtk::manage(new Gtk::Label("流氓软件查杀器")), Gtk::PACK_SHRINK);
		Gtk::ScrolledWindow * sw =
		Gtk::manage(new Gtk::ScrolledWindow);
		sw->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
		vbox0->pack_start(*sw);
		if (NULL != sw) {
			sw->set_border_width(4);
			sw->add(list_view_);
			list_model_ = Gtk::ListStore::create(list_columns_);
			list_view_.set_model(list_model_);
			list_view_.append_column_editable("选择", list_columns_.select_);
			list_view_.append_column("名称", list_columns_.name_);
			list_view_.append_column("危险度", list_columns_.harm_);
		}
		vbox0->pack_start(progress_, Gtk::PACK_SHRINK);
		Gtk::HBox *hbox0 = Gtk::manage(new Gtk::HBox);
		hbox0->set_spacing(5);
		vbox0->pack_start(*hbox0, Gtk::PACK_SHRINK);
		Gtk::Button * scan = Gtk::manage(new Gtk::Button("扫描系统"));
		hbox0->pack_start(*scan, Gtk::PACK_EXPAND_PADDING);
		scan->signal_clicked().connect(sigc::mem_fun(*this, &Main_Form::on_scan_button_clicked));
		Gtk::Button * kill = Gtk::manage(new Gtk::Button("清除目标"));
		hbox0->pack_start(*kill, Gtk::PACK_EXPAND_PADDING);
		kill->signal_clicked().connect(sigc::mem_fun(*this, &Main_Form::on_kill_button_clicked));
		vbox0->pack_start(status_bar_, Gtk::PACK_SHRINK);
		std::stringstream mark;
		mark << "流氓软件清理-预览版（可清理" << sw_scanner_->get_item_total() << "种流氓软件）";
		status_bar_.push(mark.str(), context_id_);
	}
	show_all_children();
}

Main_Form::~Main_Form()
{
}

void
Main_Form::on_scan_button_clicked()
{
	int	progress = 0;
	int	item = 0;
	int target_num = sw_scanner_->get_item_total();
	Spyware_Scanner::Spyware_Sign sign;
	list_model_->clear();
	progress_.set_fraction(0.0);
	if (NULL != sw_scanner_) {
		if (sw_scanner_->find_first(sign)) {
			do {
				if (!(sign.first.empty() && HARM_UNKNOW == sign.second)) {
					Gtk::TreeModel::Row row = *(list_model_->append());
					row[list_columns_.select_] = false;
					row[list_columns_.name_] = sign.first;
					row[list_columns_.harm_] = HARM_MIDDLE == sign.second ? "中" : "高";
					++item;
				}
				if (0 != target_num)	progress_.set_fraction(++progress / target_num);
			} while (sw_scanner_->find_next(sign));
		}
	}
	std::stringstream result;
	if (0 == item) {
		result << "恭喜，未发现流氓软件。";
	} else {
		result << "找到" << item << "个流氓软件！建议清理。";
	}
	status_bar_.pop();
	status_bar_.push(result.str(), context_id_);
}

void
Main_Form::on_kill_button_clicked()
{
	Gtk::MessageDialog no_killed(*this, "提示", false,
	Gtk::MESSAGE_INFO);
	no_killed.set_secondary_text(
	  "预览版不提供清理功能，请关注正式版的发布。");
	no_killed.run();
}
